﻿using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using log4net;

namespace DDDProject.Components.ContextProviders
{
    /// <summary>
    /// Component that stores the context in CallContext data structure
    /// </summary>
    /// <remarks>This context provider is useful for full-client apps (like winforms apps)</remarks>
    public class LocalContextProvider : IContextProvider
    {
        private static readonly ILog _log = LogManager.GetLogger(typeof(LocalContextProvider));
        private const string LOCAL_CONTEXT_KEY = "DDDProject.LocalContextKey";

        public Dictionary<string, object> GetContextStore()
        {
            // Retrieve the context store
            Dictionary<string, object> contextStore = CallContext.GetData(LOCAL_CONTEXT_KEY) as Dictionary<string, object>;

            if (contextStore == null)
            {
                // No context store : create it
                contextStore = new Dictionary<string, object>();

                // And store it
                CallContext.SetData(LOCAL_CONTEXT_KEY, contextStore);
            }

            // Return the context store
            return contextStore;
        }
    }
}
